/**
 *  编辑页
 *
 * 作者 ganjun
 * 日期 2024-11-10 20:12:58
 */
layui.use(['jquery','febs', 'form', 'validate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#resource-edit'),
        $rsType = $view.find('#rsTypeSelect'),
        $rsPlace = $view.find('#rsPlaceSelect'),
        // $rsStatus = $view.find('#rsStatusSelect'),


        validate = layui.validate;

    initDictItemSelect();

    form.verify(validate);
    form.render();
    initFormValue();

    function initDictItemSelect() {
        if(resourceData){
            //资源类型
            febs.initDictItemSelect($rsType, 'RS_TYPE', resourceData.rsType, null);
            //所属场所
            febs.initDictItemSelect($rsPlace, 'RS_PLACE', resourceData.rsPlace, null);
            //状态
            // febs.initDictItemSelect($rsStatus, 'RS_STATUS', resourceData.rsStatus, null);
        }else{
            //资源类型
            febs.initDictItemSelect($rsType, 'RS_TYPE',null, null);
            //所属场所
            febs.initDictItemSelect($rsPlace, 'RS_PLACE', null, null);
            //状态
            // febs.initDictItemSelect($rsStatus, 'RS_STATUS', null, null);
        }

    }

    function initFormValue() {
        if (resourceData) {
            form.val("resource-edit-form", {
                "id": resourceData.id,
                "rsNo": resourceData.rsNo,
                "rsName": resourceData.rsName,
                "rsType": resourceData.rsType,
                "rsPlace": resourceData.rsPlace,
                "rsDirector": resourceData.rsDirector,
                // "rsStatus": resourceData.rsStatus,
            });
            /*if(resourceData.id && resourceData.id.length > 0){
                $('#rsStatusDiv').hide();
            }*/
        }

        if(rsNo){
            form.val("resource-edit-form", {
                "rsNo": rsNo,
            });
        }
    }

    form.on('submit(resource-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            data.rsTypeName = $view.find("select[name='rsType']").find("option:selected").text();
            // data.rsStatusName = $view.find("select[name='rsStatus']").find("option:selected").text();
            data.rsPlaceName = $view.find("select[name='rsPlace']").find("option:selected").text();
            data.rsDirectorName = $view.find("select[name='rsDirector']").find("option:selected").text();

            if (data.id && data.id.length > 0) {
                febs.postArray(ctx + 'resource/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-resource').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'resource', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-resource').find('#query').trigger('click');
                });
            }
        });
    }
});